package com.orhon.smartcampus.modules.material.fetcher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.material.dao.BuildingsDao;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Site;
import com.orhon.smartcampus.modules.material.gqlModel.GQLBuilding;
import com.orhon.smartcampus.modules.material.gqlModel.GQLSite;
import com.orhon.smartcampus.modules.material.mm.BuildingMapper;
import com.orhon.smartcampus.modules.material.mm.SiteMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class BuildingFetcher implements DataFetcher  {

	@Autowired
	BuildingsDao buildingsDao;

	@Autowired
	TokenService tokenService;


	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		int id = Integer.parseInt(env.getArgument("id").toString());
		String hql = "from Buildings where deleted_at = null and id = "+id+" and school_id = " + schoolId;
		Buildings item =  buildingsDao.findId(hql, null);
		if(item!=null) {
			GQLSite gqlSite = null;
			if (env.getSelectionSet().contains("site")){
				Site site = item.getSite();
				gqlSite = SiteMapper.MAPPER.toSite(site);
			}

//			GQLCampus gqlCampus = null;
//			if (env.getSelectionSet().contains("campus")){
//				Campus campus = item.getMaterialCampusByCampusId();
//				gqlCampus = CampusMapper.MAPPER.toCampus(campus);
//			}
//
//			GQLSchool GgqlSchool = null;
//			if (env.getSelectionSet().contains("school")){
//				Schools schools = item.getBaseSchoolsBySchoolId();
//				GgqlSchool = SchoolMapper.MAPPER.toSchool(schools);
//			}
			
			GQLBuilding building = BuildingMapper.MAPPER.toBuilding(item);
//			building.setSchool(GgqlSchool);
//			building.setCampus(gqlCampus);
			building.setSite(gqlSite);
			return building;
		}
		return null;
	}
}
